package leetcode.part2;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class S3 {
    public int lengthOfLongestSubstring(String s) {
        // 字符->位置
        Map<Character, Integer> map = new HashMap<>();
        int left = -1;
        int right = 0;
        int maxLen = 0;
        for (; right < s.length(); right++) {
            if (map.containsKey(s.charAt(right))) {
                // 更新最大位置
                left = Math.max(left, map.get(s.charAt(right)));
            }
            map.put(s.charAt(right), right);
            maxLen = Math.max(maxLen, right - left);


        }
        return maxLen;
    }

    @Test
    public void test() {
        String s = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(s));
    }
}
